clear all

 
set more off
use "data/dataInsects.dta"


// Data management
if 1 {

	// drop exploratory Stanford sessions
	drop if batch == 2
	
	// drop lines that contain no observations
	keep if species <= 5
	
	
	**** generate variables
	if 1 {
		gen topAnte = vAnte == 60
		bysort id: egen topTypeManyAnte = max(topAnte)
		bysort id: egen topTypeFewAnte = min(topAnte)
		
		gen topPost = vPost == 60
		bysort id: egen topTypeManyPost = max(topPost)
		bysort id: egen topTypeFewPost = min(topPost)
		
		gen topLowAnte = vAnte > 30
		bysort id: egen topTypeLowFewAnte = min(topLowAnte)
		
		bysort id: egen topTypeFewBin = max(binarydec) // for binarydec 0 is reject, 1 is accept
		replace topTypeFewBin = 1 - topTypeFewBin
		
		gen bottomAnte = vAnte == 0
		bysort id: egen bottomTypeManyAnte = max(bottomAnte)
		bysort id: egen bottomTypeFewAnte = min(bottomAnte)
		
		bysort id: egen bottomTypeFewBin = min(binarydec) // for binarydec 0 is reject, 1 is accept
		replace bottomTypeFewBin = 1 - bottomTypeFewBin
		
		gen bottomPost = vPost == 0
		bysort id: egen bottomTypeManyPost = max(bottomPost)
		bysort id: egen bottomTypeFewPost = min(bottomPost)
		
		gen untreatableTop = topTypeFewAnte == 1 & topTypeFewPost == 1
		gen untreatableBottom = bottomTypeFewAnte == 1 & bottomTypeFewPost == 1
		gen treatableTop = 1 - untreatableTop
		gen treatableBottom = 1 - untreatableBottom

		gen treatable = (topTypeFewAnte == 0 | topTypeFewPost == 0) & (bottomTypeFewAnte == 0 | bottomTypeFewPost == 0)
		
		capture drop treatment
		capture gen treatment = 1 - control
		capture gen interTrInc = treatment * highInc
		capture gen vAnteInv = 60 - vAnte

		replace male = 2 if male == .
		replace pet = 0 if pet == .
		
		gen unispecies = 10 * university + species
		
		gen weird = v1 == "R_2atEOcb5bV7Kosd" | v1 == "R_eKWNoQjeBYN07qt" | v1 == "R_7QUmW1PA2M8pzY9" ///
			| v1 == "R_bJlcGjbLrNhIaP3" | v1 == "R_6fHRigdB1fJeDmB" | v1 == "R_9stdxcQ6XetoUi9" 

		replace pet = 0 if pet == .
		
		gen agesq = age^2
	}	
		
	drop if id == . | species == .

	cap gen surplus = .
	replace surplus = 30 - vPost if highInc == 1
	replace surplus = 3  - vPost if highInc == 0
	replace surplus = 0 if binarydec == 0

	cap gen highInc_treatment = highInc * treatment
	
	gen bet = binarydec
	
	save "data/dataEdited.dta", replace
	
}


// Account for censoring in vPost
if 1 {
	
	// Assume vPost is lognormally distributed. Then estimate the 
	// conditional means using Tobit. Replace observations of vPost 
	// that are censored above with the estimated mean conditional 
	// on being above the threshold.
	
	gen logVpost = ln(vPost)
						
	su vPost, d
	local med = r(p50)
	su logVpost if vPost > `med'
	// local min = r(min)
	local max = r(max)
	
	tobit logVpost if bet == 1 & highInc == 1, ul(`max') 
	
	mat B = e(b)
	
	local mu = B[1,1]
	local sigma = sqrt(B[1,2])
	local alpha = (`max' - `mu')/`sigma'
	
	
	// gen logVpostPred = rnormal(`mu', `sigma')
	// distplot logVpost logVpostPred if bet == 1
	// graph export "graphs/surplusTobitFitKnown.pdf", replace				
	
	
	local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
	noisily di exp(`condMean')
	
	replace logVpost = `condMean' if logVpost == `max' & highInc == 1
	
	
	
	tobit logVpost if bet == 1 & highInc == 0, ul(`max') 
	
	mat B = e(b)
	
	local mu = B[1,1]
	local sigma = sqrt(B[1,2])
	local alpha = (`max' - `mu')/`sigma'
	
	/*
	gen logVpostPred = rnormal(`mu', `sigma')
	distplot line logVpost logVpostPred if bet == 1
	graph export "graphs/surplusTobitFitKnown.pdf", replace				
	*/
	
	local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
	noisily di exp(`condMean')
	
	replace logVpost = `condMean' if vPost == 60 & highInc == 0
	
	replace vPost = exp(logVpost)  if vPost == 60 
	replace vPost`benchmarkCase' = vPost
	
	tab vPost if vPost >= 60 & highInc == 0
	tab vPost if vPost >= 60 & highInc == 1
	
	
	
	save "data/dataRawRidgePred.dta", replace

}

	
// Get ridge predictions for video condition 
if 1 {
	
	// Make such that for cases in which species = 5 is missing, the prediction is based only on species 1 - 4.
	// Predict only into the available species, because there is not betting decision for the missing species. 
	
	clear 
	use "data/dataRawRidgePred.dta"
	keep if treatment == 1

	keep id species bet vPost highInc
	
	gen win = .
	replace win = 30 if highInc == 1
	replace win = 3 if highInc == 0	
	
	if 1 {
		gen missingFieldCrickets_i = species == 5 & vPost == .
		bysort id: egen missingFieldCrickets = max(missingFieldCrickets_i)
		drop missingFieldCrickets_i
	}
	
	
	rename vPost  v0_
	gen  v1_ = bet
	gen  v2_ = win
	
	
	
		forvalues i0 = 0 / 2 {
			// Levels
			if `i0' > 0 {
				su v`i0'_ 
				replace v`i0'_ = (v`i0'_ - r(mean)) / r(sd)				// Standardize predictor
			}
			// Interactions and squares
			forvalues i1 = 0 / 2 {
				gen v`i0'`i1'_ = v`i0'_ * v`i1'_
				su v`i0'`i1'_
				replace v`i0'`i1'_ = (v`i0'`i1'_ - r(mean)) / r(sd)		// Standardize predictor
				forvalues i2 = 0 / 2 {
					gen v`i0'`i1'`i2'_ = v`i0'_ * v`i1'_ * v`i2'_
					su v`i0'`i1'`i2'_ 
					replace v`i0'`i1'`i2'_ = (v`i0'`i1'`i2'_ - r(mean)) / r(sd)	// Standardize predictor
				}
			}			
		}

			reshape wide v*_ bet, i(id) j(species)

	set seed 1
	forvalues i = 1 / 5 {

		preserve 
			rename v0_`i' depVar
			gen v0_`i' = .
			drop v*0*_`i'	// Drop all variables that include the dependent variable
			drop v*1*_`i'	// Drop all variables that include the betting decision corresponding to the current dependent variable
			elasticnet linear depVar v*, alphas(0)  // Alpha is the weight on the lasso penalty, so alpha = 0 is ridge-regression		
		restore

		predict depVarPred`i' if missingFieldCrickets == 0
		
		if 1 {
			preserve 
				rename v0_`i' depVar
				gen v0_`i' = .
				drop v*0*_`i'	// Drop all variables that include the dependent variable
				drop v*1*_`i'	// Drop all variables that include the betting decision corresponding to the current dependent variable
				cap drop v*0*_5		// Drop all variables that include field crickets
				cap drop v*1*_5		// Drop all variables that include the betting decision corresponding to field crickets
				elasticnet linear depVar v*, alphas(0)  // Alpha is the weight on the lasso penalty, so alpha = 0 is ridge-regression		
			restore

			predict depVarPred`i'_missingFieldCrickets if missingFieldCrickets == 1
			
			replace depVarPred`i' = depVarPred`i'_missingFieldCrickets if missingFieldCrickets == 1
		}
		
		
		gen betCEpred`i' = (win - depVarPred`i') * bet`i'
		
		if 1 {
			replace betCEpred`i' = . if `i' == 5 & missingFieldCrickets == 1
			replace depVarPred`i' = . if `i' == 5 & missingFieldCrickets == 1
		}
			
		gen gain`i' = max(betCEpred`i', 0)	
		gen loss`i' = min(betCEpred`i', 0)
		
		if 1 {
			replace gain`i' = . if betCEpred`i' == .
			replace loss`i' = . if betCEpred`i' == .
		}
		
		rename depVarPred`i' vPostPred`i'

	}	
	
	reshape long gain loss betCEpred vPostPred, i(id) j(species)	
	rename betCEpred  betCEpredVid
	
	keep id species betCEpredVid vPostPred missingFieldCrickets
	rename vPostPred vPostPredVid


	save "data/ridgePredVideo.dta", replace
	
}
	

// Get ridge predictions for no-video condition
if 1 {
	clear 
	use "data/dataRawRidgePred.dta"
	keep if treatment == 0

	keep id species bet vPost highInc
	
	gen win = .
	replace win = 30 if highInc == 1
	replace win = 3 if highInc == 0	

	if 1 {
		gen missingFieldCrickets_i = species == 5 & vPost == .
		bysort id: egen missingFieldCrickets = max(missingFieldCrickets_i)
		drop missingFieldCrickets_i
	}


	rename vPost  v0_
	gen  v1_ = bet
	gen  v2_ = win
	
	
		forvalues i0 = 0 / 2 {
			if `i0' > 0 {
				su v`i0'_ 
				replace v`i0'_ = (v`i0'_ - r(mean)) / r(sd)
			}
			forvalues i1 = 0 / 2 {
				gen v`i0'`i1'_ = v`i0'_ * v`i1'_
				su v`i0'`i1'_
				replace v`i0'`i1'_ = (v`i0'`i1'_ - r(mean)) / r(sd)
				forvalues i2 = 0 / 2 {
					gen v`i0'`i1'`i2'_ = v`i0'_ * v`i1'_ * v`i2'_
					su v`i0'`i1'`i2'_ 
					replace v`i0'`i1'`i2'_ = (v`i0'`i1'`i2'_ - r(mean)) / r(sd)
				}
			}			
		}
	
	
	reshape wide v*_ bet, i(id) j(species)

	set seed 1
	forvalues i = 1 / 5 {

		preserve 
			rename v0_`i' depVar
			gen v0_`i' = .
			drop v*0*_`i'	// Drop all variables that include the dependent variable
			drop v*1*_`i'	// Drop all variables that include the betting decision corresponding to the current dependent variable
			elasticnet linear depVar v*, alphas(0)  // Alpha is the weight on the lasso penalty, so alpha = 0 is ridge-regression		
		restore

		predict depVarPred`i'
		
		if 1 {
			preserve 
				rename v0_`i' depVar
				gen v0_`i' = .
				drop v*0*_`i'	// Drop all variables that include the dependent variable
				drop v*1*_`i'	// Drop all variables that include the betting decision corresponding to the current dependent variable
				cap drop v*0*_5		// Drop all variables that include field crickets
				cap drop v*1*_5		// Drop all variables that include the betting decision corresponding to field crickets
				elasticnet linear depVar v*, alphas(0)  // Alpha is the weight on the lasso penalty, so alpha = 0 is ridge-regression		
			restore

			predict depVarPred`i'_missingFieldCrickets if missingFieldCrickets == 1
			
			replace depVarPred`i' = depVarPred`i'_missingFieldCrickets if missingFieldCrickets == 1
		}
		
		gen betCEpred`i' = (win - depVarPred`i') * bet`i'
		

		if 1 {
			replace betCEpred`i' = . if `i' == 5 & missingFieldCrickets == 1
		}
		
		gen gain`i' = max(betCEpred`i', 0)	
		gen loss`i' = min(betCEpred`i', 0)
		
		if 1 {
			replace gain`i' = . if betCEpred`i' == .
			replace loss`i' = . if betCEpred`i' == .
		}
		
		rename depVarPred`i' vPostPred`i'

	}	
	
	reshape long gain loss betCEpred vPostPred, i(id) j(species)	
	
	rename betCEpred betCEpredNoVid
	rename vPostPred vPostPredNoVid
	
	keep id species betCEpredNoVid vPostPredNoVid
	
	save "data/ridgePredNoVideo.dta", replace
	
}


// Merge in the data
if 1 {
	clear
	use "data/dataRawRidgePred.dta", replace
	merge 1:1 id species using "data/ridgePredNoVideo.dta"
	cap drop _m
	merge 1:1 id species using "data/ridgePredVideo.dta"

	cap drop win gain loss
	cap gen win = . 
	replace win = 30 if highInc == 1 
	replace win = 3 if highInc == 0 
	cap gen betCE = .
	replace betCE = betCEpredVid if treatment == 1
	replace betCE = betCEpredNoVid if treatment == 0	
	cap gen gain = .
	cap gen loss = . 
	replace gain = max(0, betCE)
	replace loss = min(0, betCE)
	cap gen mid = (gain + loss)/2
	
	drop vPost
	gen vPost = .
	replace vPost = vPostPredVid if treatment == 1
	replace vPost = vPostPredNoVid if treatment == 0	
	
	save "data/dataRidgePredNonmatched.dta", replace

}
	

// Match the data (with ridge)
if 1 {

	use "data/dataRidgePredNonmatched.dta", replace

	cap gen condNr = .
	replace condNr = 1 if treatment == 0 & highInc == 0
	replace condNr = 2 if treatment == 1 & highInc == 0
	replace condNr = 3 if treatment == 0 & highInc == 1
	replace condNr = 4 if treatment == 1 & highInc == 1

	// By species: Match by WTA rank
	// Issue: some people might have revealed the WTA level, 
	//		but might get matched to different WTA levels, 
	//		and some might have accepted while others rejected.
	// 		Hence, consider worst-case and best-case: sort by WTA and acceptance, 
	// 		and also sort by WTA and rejection for a second match
	
	cap gen noBet = 1 - bet	
		
	sort treatment highInc species vPost bet
	order treatment highInc species vPost bet
	
	by treatment highInc species: egen rank = rank(vPost), unique
	
	// Map WTA for condNr = 1 to all other condNr
	
	forvalues i = 1 / 4 {
		su rank if condNr == `i'
		local max`i' = r(max)
	}
	
	// make the ranks for all other condNr conform to those for condNr == i
	forvalues i = 1 / 4 {
		gen rank`i' = .
		forvalues j = 1 / 4 { 
			replace rank`i' = round((`max`i''/`max`j'')*rank, 1) if condNr == `j'
		}
	}
	
	// Copy the WTA info for condNr i to condNr j
	forvalues i = 1 / 4 {
		preserve
			keep if condNr == `i'
			keep rank`i' vPost species
			drop if vPost == . | species == . | rank`i' == . 
			rename vPost vPost`i'
			save "data/wtaPredRank`i'.dta", replace
		restore
	}
	
	cap drop _m
	forvalues i = 1 / 4 {
		merge m:1 rank`i' species using "data/wtaPredRank`i'.dta"
		drop _m
	}
	
	save "data/dataRidgePredMatched.dta", replace
	
}
	
	
// Match the data (without ridge)
if 1 {

	use "data/dataRawRidgePred.dta", replace

	cap gen condNr = .
	replace condNr = 1 if treatment == 0 & highInc == 0
	replace condNr = 2 if treatment == 1 & highInc == 0
	replace condNr = 3 if treatment == 0 & highInc == 1
	replace condNr = 4 if treatment == 1 & highInc == 1

	// By species: Match by WTA rank
	// Issue: some people might have revealed the WTA level, 
	//		but might get matched to different WTA levels, 
	//		and some might have accepted while others rejected.
	// 		Hence, consider worst-case and best-case: sort by WTA and acceptance, 
	// 		and also sort by WTA and rejection for a second match
	
	cap gen noBet = 1 - bet	
		
	sort treatment highInc species vPost bet
	order treatment highInc species vPost bet
	
	by treatment highInc species: egen rank = rank(vPost), unique
	
	// Map WTA for condNr = 1 to all other condNr
	
	forvalues i = 1 / 4 {
		su rank if condNr == `i'
		local max`i' = r(max)
	}
	
	// make the ranks for all other condNr conform to those for condNr == i
	forvalues i = 1 / 4 {
		gen rank`i' = .
		forvalues j = 1 / 4 { 
			replace rank`i' = round((`max`i''/`max`j'')*rank, 1) if condNr == `j'
		}
	}
	
	// Copy the WTA info for condNr i to condNr j
	forvalues i = 1 / 4 {
		preserve
			keep if condNr == `i'
			keep rank`i' vPost species
			drop if vPost == . | species == . | rank`i' == . 
			rename vPost vPost`i'
			save "data/wtaPredRank`i'.dta", replace
		restore
	}
	
	cap drop _m
	forvalues i = 1 / 4 {
		merge m:1 rank`i' species using "data/wtaPredRank`i'.dta"
		drop _m
	}
	
	save "data/dataNoRidgePredMatched.dta", replace
}


// Table 1
if 1 {


	cap postclose tableInsects
	postfile tableInsects l0 l1 g0 g1 alphaLower alphaUpper p N N_clust str2 kind using "data/welfareMainInsects.dta", replace

	local bdraws = 1000
	set seed 1

	// Only accounting for censoring
	if 1 {
		use "data/dataRawRidgePred.dta", replace

		cap drop win gain loss
		cap gen betCE = bet*surplus
		gen gain = max(0, betCE)
		gen loss = min(0, betCE)
		replace gain = . if betCE == .
		replace loss = . if betCE == .

		
		
		bootstrap, cluster(id) reps(`bdraws'): ///
		reg3 (loss highInc) ///
			 (gain  highInc) ///
			 if treatment == 1, ols
		local N_clust = e(N_clust)
		local N = e(N)
		
			 
			 
		local l0 = [loss]_b[_cons]
		local se_l0 = [loss]_se[_cons]
		local g0 = [gain]_b[_cons]
		local se_g0 = [gain]_se[_cons]
			 
		lincom [loss]_cons + [loss]highInc
		local l1 = r(estimate)
		local se_l1 = r(se)

		lincom [gain]_cons + [gain]highInc
		local g1 = r(estimate)
		local se_g1 = r(se)

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons )
		matrix b = r(b)
		matrix V = r(V)
		local alphaUpper = b[1,1]
		local se_alphaUpper = sqrt(V[1,1])

		nlcom ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local alphaLower = b[1,1]
		local se_alphaLower = sqrt(V[1,1])

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons ) - ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local std_err = sqrt(V[1,1])
		local z = b[1,1]/`std_err'
		local p = 2*normal(-abs(`z'))

		post tableInsects (`l0') (`l1') (`g0') (`g1') (`alphaUpper') (`alphaLower') (`p') (`N_clust') (`N') ("b")
		post tableInsects (`se_l0') (`se_l1') (`se_g0') (`se_g1') (`se_alphaUpper') (`se_alphaLower') (.) (.) (.) ("se")
	}
	
	// Only accounting for censoring and noise 
	if 1 {
		use "data/dataRidgePredNonmatched.dta", replace


		cap drop win gain loss surplus
		gen surplus = .
		replace surplus = 30 - vPost if highInc == 1
		replace surplus = 3 - vPost if highInc == 0
		replace surplus = bet * surplus

		gen gain = max(0, surplus)
		gen loss = min(0, surplus)
		replace gain = . if surplus == .
		replace loss = . if surplus == .

		bootstrap, cluster(id) reps(`bdraws'): ///
		reg3 (loss highInc) ///
			 (gain  highInc) ///
			 if treatment == 1, ols
		local N_clust = e(N_clust)
		local N = e(N)

		local l0 = [loss]_b[_cons]
		local se_l0 = [loss]_se[_cons]
		local g0 = [gain]_b[_cons]
		local se_g0 = [gain]_se[_cons]
			 
		lincom [loss]_cons + [loss]highInc
		local l1 = r(estimate)
		local se_l1 = r(se)

		lincom [gain]_cons + [gain]highInc
		local g1 = r(estimate)
		local se_g1 = r(se)

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons )
		matrix b = r(b)
		matrix V = r(V)
		local alphaUpper = b[1,1]
		local se_alphaUpper = sqrt(V[1,1])

		nlcom ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local alphaLower = b[1,1]
		local se_alphaLower = sqrt(V[1,1])

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons ) - ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local std_err = sqrt(V[1,1])
		local z = b[1,1]/`std_err'
		local p = 2*normal(-abs(`z'))

		post tableInsects (`l0') (`l1') (`g0') (`g1') (`alphaUpper') (`alphaLower') (`p') (`N_clust') (`N') ("b")
		post tableInsects (`se_l0') (`se_l1') (`se_g0') (`se_g1') (`se_alphaUpper') (`se_alphaLower') (.) (.) (.) ("se")	
	
	}
	
	// Only accounting for censoring and anchoring
	if 1 {
		use "data/dataNoRidgePredMatched.dta", replace

		keep if treatment == 1
		
		
		drop surplus 
		
		local benchmarkCase = 1
		cap gen surplus = .
		replace surplus = 30 - vPost1 if highInc == 1
		replace surplus = 3  - vPost1 if highInc == 0
		replace surplus = 0 if binarydec == 0


		cap drop win* gain loss
		gen gain = max(0, surplus)
		gen loss = min(0, surplus)
		replace gain = . if surplus == .
		replace loss = . if surplus == .
		
	

		bootstrap, cluster(id) reps(`bdraws'): ///
		reg3 (loss highInc) ///
			 (gain  highInc) ///
			 if treatment == 1, ols
		local N_clust = e(N_clust)
		local N = e(N)

		local l0 = [loss]_b[_cons]
		local se_l0 = [loss]_se[_cons]
		local g0 = [gain]_b[_cons]
		local se_g0 = [gain]_se[_cons]
			 
		lincom [loss]_cons + [loss]highInc
		local l1 = r(estimate)
		local se_l1 = r(se)

		lincom [gain]_cons + [gain]highInc
		local g1 = r(estimate)
		local se_g1 = r(se)

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons )
		matrix b = r(b)
		matrix V = r(V)
		local alphaUpper = b[1,1]
		local se_alphaUpper = sqrt(V[1,1])

		nlcom ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local alphaLower = b[1,1]
		local se_alphaLower = sqrt(V[1,1])

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons ) - ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local std_err = sqrt(V[1,1])
		local z = b[1,1]/`std_err'
		local p = 2*normal(-abs(`z'))

		post tableInsects (`l0') (`l1') (`g0') (`g1') (`alphaUpper') (`alphaLower') (`p') (`N_clust') (`N') ("b")
		post tableInsects (`se_l0') (`se_l1') (`se_g0') (`se_g1') (`se_alphaUpper') (`se_alphaLower') (.) (.) (.) ("se")
	
	}
	
	// Accounting for censoring, noise, and anchoring
	if 1 {
		use "data/dataRidgePredMatched.dta", replace
		
		keep if treatment == 1
		
		
		drop surplus 
		
		local benchmarkCase = 1
		cap gen surplus = .
		replace surplus = 30 - vPost1 if highInc == 1
		replace surplus = 3  - vPost1 if highInc == 0
		replace surplus = 0 if binarydec == 0
		

		cap drop win* gain loss
		gen gain = max(0, surplus)
		gen loss = min(0, surplus)
		replace gain = . if surplus == .
		replace loss = . if surplus == .

		bootstrap, cluster(id) reps(`bdraws'): ///
		reg3 (loss highInc) ///
			 (gain  highInc) ///
			 if treatment == 1, ols
		local N_clust = e(N_clust)
		local N = e(N)

		local l0 = [loss]_b[_cons]
		local se_l0 = [loss]_se[_cons]
		local g0 = [gain]_b[_cons]
		local se_g0 = [gain]_se[_cons]
			 
		lincom [loss]_cons + [loss]highInc
		local l1 = r(estimate)
		local se_l1 = r(se)

		lincom [gain]_cons + [gain]highInc
		local g1 = r(estimate)
		local se_g1 = r(se)

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons )
		matrix b = r(b)
		matrix V = r(V)
		local alphaUpper = b[1,1]
		local se_alphaUpper = sqrt(V[1,1])

		nlcom ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local alphaLower = b[1,1]
		local se_alphaLower = sqrt(V[1,1])

		nlcom [gain]_cons  / ([gain]_cons  - [loss]_cons ) - ([gain]_cons + [gain]highInc) / ([gain]_cons + [gain]highInc  - [loss]_cons - [loss]highInc)
		matrix b = r(b)
		matrix V = r(V)
		local std_err = sqrt(V[1,1])
		local z = b[1,1]/`std_err'
		local p = 2*normal(-abs(`z'))

		post tableInsects (`l0') (`l1') (`g0') (`g1') (`alphaUpper') (`alphaLower') (`p') (`N_clust') (`N') ("b")
		post tableInsects (`se_l0') (`se_l1') (`se_g0') (`se_g1') (`se_alphaUpper') (`se_alphaLower') (.) (.) (.) ("se")
	
	}
	
	postclose tableInsects
	
	// Format the table
	if 1 {	
		use "data/welfareMainInsects", replace
		gen se = kind == "se"
		drop kind
		gen id = floor((_n-1)/2)
		reshape wide l0 l1  g0 g1 alphaUpper alphaLower p N N_clust, i(id) j(se)
		drop id
		xpose, clear varname
		drop if _varname == "p1"

		gen sortOrder = .
		replace sortOrder =  1 if _varname == "l00"
		replace sortOrder =  2 if _varname == "l01"
		replace sortOrder =  3 if _varname == "l10"
		replace sortOrder =  4 if _varname == "l11"
		replace sortOrder =  5 if _varname == "g00"
		replace sortOrder =  6 if _varname == "g01"
		replace sortOrder =  7 if _varname == "g10"
		replace sortOrder =  8 if _varname == "g11"
		replace sortOrder =  9 if _varname == "alphaLower0"
		replace sortOrder = 10 if _varname == "alphaLower1"
		replace sortOrder = 11 if _varname == "alphaUpper0"
		replace sortOrder = 12 if _varname == "alphaUpper1"
		replace sortOrder = 13 if _varname == "p0"
		sort sortOrder
		drop sortOrder

		order _varname
		rename _varname name
		
		replace name = "$ l(3)$" 				if name == "l00"
		replace name = "$ l(30)$" 				if name == "l10"
		replace name = "$ g(3)$" 				if name == "g00"
		replace name = "$ g(30)$" 				if name == "g10"
		replace name = "\quad max for \$3 acceptable, $\overline{\alpha}$"	if name == "alphaLower0"
		replace name = "\quad min for \$30 unacceptable, $\underline{\alpha}$"	if name == "alphaUpper0"
		replace name = "$ p$-value $\underline{\alpha} = \overline{\alpha}$"					if name == "p0"
		replace name = "Subjects"				if name == "N0"
		replace name = "Observations"			if name == "N_clust0"
		
		replace name = " " 	if inlist(name, "l01", "l11", "g01", "g11", "alphaLower1", "alphaUpper1")
		drop if inlist(name, "N1", "N_clust1")
		
		
		gen c1 = "&"
		gen c2 = "&"
		gen c3 = "&"
		gen c4 = "&"
		gen c5 = "\\"
		
		replace c1 = "& (" if name == " "
		replace c2 = ") & (" if name == " "
		replace c3 = ") & (" if name == " "
		replace c4 = ") & (" if name == " "
		replace c5 = ") \\" if name == " "
		// replace c5 = " "	if name == "Observations"
		
		order name c1 v1 c2 v3 c3 v2 c4 v4 c5
		
		gen id = _n
		gen idNew = id
		
		replace idNew = idNew - 14 if id >= 9 & id <= 13
		sort idNew
		
		replace c5 = "\\ \\" if idNew == -1
		
		drop id idNew

		
		gen id = _n
		replace id = 3 if _n == 1
		replace id = 4 if _n == 2
		replace id = 1 if _n == 3
		replace id = 2 if _n == 4
		sort id
		drop id
		
		drop if _n >= 6 & _n <= 13
		
		format v* %9.3f
		
		
		* Open the output file for writing
		cap file close myfile
		file open myfile using "../writeup/tables/Table1.tsv", write replace
		
		local format = "%9.3f"
		
		local n = 1
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 
		
		file write myfile "\quad min for \ $30 unacceptable, $\underline{\alpha}$ & `v1' & `v2' & `v3' & `v4'  \\ " 
		
		local n = 2
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 

		file write myfile "`labl' & (`v1') & (`v2') & (`v3') & (`v4')  \\ " 

		local n = 3
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 
		
		file write myfile "\quad max for \ $3 acceptable, $\overline{\alpha}$ & `v1' & `v2' & `v3' & `v4'  \\ " 
		
		local n = 4
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 

		file write myfile "`labl' & (`v1') & (`v2') & (`v3') & (`v4')  \\ " 
		
		
		file write myfile "UIH-normative satisfied & No & No & No & No \\ " 

		
		local n = 5
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 
		
		file write myfile "`labl' & `v1' & `v2' & `v3' & `v4'  \\ " 
		
		local format = "%9.0f"

		local n = 6
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 
		
		file write myfile "`labl' & `v1' & `v2' & `v3' & `v4'  \\ " 
		
		
		local n = 7
		local labl = name[`n']
		local v1   : display `format'  v1[`n'] 
		local v2   : display `format'  v2[`n'] 
		local v3   : display `format'  v3[`n'] 
		local v4   : display `format'  v4[`n'] 
		
		file write myfile "`labl' & `v1' & `v2' & `v3' & `v4'  \\ " 
		
		
		
		file close myfile
		
	}
	
}


// Supplementary Table A.4.B: Make table same-condition benchmark
// and 
// Supplementary Table A.4.D: Welfare table No Video, Low Incentive benchmark
if 1 {
		
		
	// Merge in the data
	if 1 {
		clear
		use "data/dataRawRidgePred.dta", replace
		merge 1:1 id species using "data/ridgePredNoVideo.dta"
		cap drop _m
		merge 1:1 id species using "data/ridgePredVideo.dta"


		
		cap drop win gain loss
		cap gen win = . 
		replace win = 30 if highInc == 1 
		replace win = 3 if highInc == 0 
		cap gen betCE = .
		replace betCE = betCEpredVid if treatment == 1
		replace betCE = betCEpredNoVid if treatment == 0	
		cap gen gain = .
		cap gen loss = . 
		replace gain = max(0, betCE)
		replace loss = min(0, betCE)
		cap gen mid = (gain + loss)/2
		
		drop vPost
		gen vPost = .
		replace vPost = vPostPredVid if treatment == 1
		replace vPost = vPostPredNoVid if treatment == 0
		
		
	}

	// Make table same-condition benchmark (Supplementary Table A.4.B)
	if 1 {
		
		cap gen winMin = win - 1
		replace winMin = . if win == 0
		
		gen condition = species + 5*control

		mysureg (gain win) /// 
				(loss win) if treatment == 1 ///
			, vce(cluster id)
			
		// Minimal required psych benefit
		nlcom  [loss]_cons * ( [gain]win / [loss]win ) - [gain]_cons
			// No such number exists, because losses do not get worse with the incentive
			

		mysureg (gain win) /// 
				(loss win) if treatment == 0 ///
			, vce(cluster id)
			
		// Minimal required psych benefit
		nlcom  [loss]_cons * ( [gain]win / [loss]win ) - [gain]_cons
			// The estimate of the intercept term for gains would need to be higher 
			// by USD 67.70
			
					
		



		cap postclose welfareTable
		postfile welfareTable ///
				 aG str3 starsGainA double aL str3 starsLossA ///
				 bG str3 starsGain double bL str3 starsLoss ///
				 double alphaMin str4 starsAlpha ///
				 double alphaMax str4 starsAlphaMax ///
				 str4 cond double p ///
				 using "data/welfareTableRidge.dta", replace
				 
		foreach species of numlist 1/10 {
			preserve 
				
				keep if condition == `species'

				cap drop surplus
			
				rename betCE surplus
				
				sureg (gain win) /// 
						(loss win) 
				
				
				local mMin = 3
				local mMax = 30
				
				// Condition

				nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
							- ([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win))) ///
						(eq2: (([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win)))) ///	
						(eq3: ([gain]_cons + `mMin'*[gain]win)) ///
						(eq4: ([loss]_cons + `mMin'*[loss]win)) ///
						(eq5: (([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win)))) ///
						(eq6: ([gain]_cons + `mMax'*[gain]win)) ///
						(eq7: ([loss]_cons + `mMax'*[loss]win)) ///
					, post
				
				// New
				
				local bG = _b[eq6]
				local bL = _b[eq7]
				local seBg = _se[eq6] 
				local seBl = _se[eq7]
				
				test eq6 == 0
				local pGain = r(p)
				test eq7 == 0
				local pLoss = r(p)
				
				
				local starsGain = ""
				if (`pGain' <= 0.01) {
					local starsGain = "***"
				}
				if (`pGain' <= 0.05 & `pGain'>0.01) {
					local starsGain = "**"
				}
				if (`pGain' < 0.1 & `pGain'>0.05) {
					local starsGain = "*"
				}
				
				
				local starsLoss = ""
				if (`pLoss' <= 0.01) {
					local starsLoss = "***"
				}
				if (`pLoss' <= 0.05 & `pLoss'>0.01) {
					local starsLoss = "**"
				}
				if (`pLoss' < 0.1 & `pLoss'>0.05) {
					local starsLoss = "*"
				}
				
				// Previous
				
				local alphaMin = _b[eq2]
				local alphaSE = _se[eq2]
				local alphaMax = _b[eq5]
				local alphaMaxSE = _se[eq5]
				
				test eq2 == 0
				local pAlpha = r(p)
				
				local starsAlpha = ""
				if (`pAlpha' <= 0.01) {
					local starsAlpha = "***"
				}
				if (`pAlpha' <= 0.05 & `pAlpha'>0.01) {
					local starsAlpha = "**"
				}
				if (`pAlpha' < 0.1 & `pAlpha'>0.05) {
					local starsAlpha = "*"
				}

				local possibilityCond = "No"
				if (_b[eq1] >= 0) {
					local possibilityCond = "Yes"
				}
				
				
				test eq5 == 1
				local pAlphaMax = r(p)
				
				local starsAlphaMax = ""
				if (`pAlphaMax' <= 0.01) {
					local starsAlphaMax = "***"
				}
				if (`pAlphaMax' <= 0.05 & `pAlpha'>0.01) {
					local starsAlphaMax = "**"
				}
				if (`pAlphaMax' < 0.1 & `pAlpha'>0.05) {
					local starsAlphaMax = "*"
				}

				local possibilityCond = "No"
				if (_b[eq1] >= 0) {
					local possibilityCond = "Yes"
				}
				
				
				local aG = _b[eq3]
				local aGse = _se[eq3]
				local aL = _b[eq4]
				local aLse = _se[eq4]
				
				
				
				test _b[eq3] == 0
				local pGainA = r(p)
				test _b[eq4] == 0
				local pLossA = r(p)
				
				
				local starsGainA = ""
				if (`pGainA' <= 0.01) {
					local starsGainA = "***"
				}
				if (`pGainA' <= 0.05 & `pGainA'>0.01) {
					local starsGainA = "**"
				}
				if (`pGainA' < 0.1 & `pGainA'>0.05) {
					local starsGainA = "*"
				}
				
				
				local starsLossA = ""
				if (`pLossA' <= 0.01) {
					local starsLossA = "***"
				}
				if (`pLossA' <= 0.05 & `pLossA'>0.01) {
					local starsLossA = "**"
				}
				if (`pLossA' < 0.1 & `pLossA'>0.05) {
					local starsLossA = "*"
				}
				
				
				noisily: di "`possibilityCond'"
						
				test eq1
				local pTest = r(p)

				
			restore
			post welfareTable ///
				(`aG') ("`starsGainA'") (`aL') ("`starsLossA'")  ///
				(`bG') ("`starsGain'") (`bL') ("`starsLoss'") ///
				(`alphaMin') ("`starsAlpha'") (`alphaMax') ("`starsAlphaMax'") ///
				("`possibilityCond'") (`pTest')
							
			post welfareTable ///
				(`aGse') ("") (`aLse') ("") ///
				(`seBg') ("") (`seBl') ("") ///
				(`alphaSE') ("") (`alphaMaxSE') ("") ("") (.)
			

			
		}
		postclose welfareTable
		
		// Format the table (Supplementary Table A.4.B)
		preserve
			clear
			use "data/welfareTableRidge.dta"
							
			gen c1 = "("
			replace c1 = "" if mod(_n,2) == 1
			
			gen c2 = ") & ("
			replace c2 = "  &  " if mod(_n,2) == 1
			
			gen c3 = c2
			gen c4 = c2
			gen c5 = c2
			gen c6 = c2
			
			gen c7 = ") & "
			replace c7 = "  &  " if mod(_n,2) == 1
			
			gen c8 = " & " 
			
			gen c9 = " \\ "
			
			order c1 aG starsGainA c2 ///
					 aL starsLossA c3 ///
					 alphaMax starsAlphaMax c6 ///
					 bG starsGain  c4 ///
					 bL starsLoss  c5 /// 
					 alphaMin starsAlpha c7 ///
					 cond c8 p c9
			
			
			// Formatting the table
			
			/*
			Condition
			2 house crickets
			5 superworms
			3 silkworm pupae
			2 mole crickets
			2 field crickets
			*/
			
			gen rowlabel = "&"
			replace rowlabel = "2 house crickets	&" 	if mod(_n, 10) == 1 
			replace rowlabel = "5 superworms	&" 		if mod(_n, 10) == 3
			replace rowlabel = "3 silkworm pupae	&" 	if mod(_n, 10) == 5
			replace rowlabel = "2 mole crickets	&" 	if mod(_n, 10) == 7
			replace rowlabel = "2 field crickets	&" 	if mod(_n, 10) == 9
			
			local lines = _N
			local newLines = `lines' + 2
			di `newLines'
			set obs `newLines'
			
			replace rowlabel = "\textit{Video} \\" 		if _n == 21
			replace rowlabel = "\textit{No Video} \\" 	if _n == 22
			
			gen location = _n
			replace location = 0 		if location == 21
			replace location = 10.5 	if location == 22
			
			drop if location > 10
			drop if location == 0

			sort location
			drop location
			order rowlabel
			
			
			format aG aL bG bL alphaMin alphaMax p %9.3f
			export delimited using "../writeup/tables/SupplementaryTableA4B.tsv", delimiter(tab)  datafmt novar replace
							
		restore
		
		
		
		
		
	}

	// Match the data 
	if 1 {


		cap gen condNr = .
		replace condNr = 1 if treatment == 0 & highInc == 0
		replace condNr = 2 if treatment == 1 & highInc == 0
		replace condNr = 3 if treatment == 0 & highInc == 1
		replace condNr = 4 if treatment == 1 & highInc == 1

		// By species: Match by WTA rank
		// Issue: some people might have revealed the WTA level, 
		//		but might get matched to different WTA levels, 
		//		and some might have accepted while others rejected.
		// 		Hence, consider worst-case and best-case: sort by WTA and acceptance, 
		// 		and also sort by WTA and rejection for a second match
		
		cap gen noBet = 1 - bet	
			
		sort treatment highInc species vPost bet
		order treatment highInc species vPost bet
		
		by treatment highInc species: egen rank = rank(vPost), unique
		
		// Map WTA for condNr = 1 to all other condNr
		
		forvalues i = 1 / 4 {
			su rank if condNr == `i'
			local max`i' = r(max)
		}
		
		// make the ranks for all other condNr conform to those for condNr == i
		forvalues i = 1 / 4 {
			gen rank`i' = .
			forvalues j = 1 / 4 { 
				replace rank`i' = round((`max`i''/`max`j'')*rank, 1) if condNr == `j'
			}
		}
		
		// Copy the WTA info for condNr i to condNr j
		forvalues i = 1 / 4 {
			preserve
				keep if condNr == `i'
				keep rank`i' vPost species
				drop if vPost == . | species == . | rank`i' == . 
				rename vPost vPost`i'
				save "data/wtaPredRank`i'.dta", replace
			restore
		}
		
		cap drop _m
		forvalues i = 1 / 4 {
			merge m:1 rank`i' species using "data/wtaPredRank`i'.dta"
			drop _m
		}
		
	}
		
	// Make the table (Supplementary Table A.4.D)
		// Welfare table No Video, Low Incentive benchmark
		if 1 {
			
			cap drop win gain loss
			
			drop surplus 
			
			local benchmarkCase = 1
			cap gen surplus = .
			replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
			replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
			replace surplus = 0 if binarydec == 0
			
		
			cap gen win = . 
			replace win = 30 if highInc == 1 
			replace win = 3 if highInc == 0
			cap drop betCE
			gen betCE = binarydec * surplus
			cap gen gain = .
			cap gen loss = . 
			replace gain = max(0, betCE)
			replace loss = min(0, betCE)
			cap gen mid = (gain + loss)/2
			
			cap gen winMin = win - 1
			replace winMin = . if win == 0
			
			cap drop condition
			gen condition = species + 5*control

			

		

			cap postclose welfareTable
			postfile welfareTable ///
					 aG str3 starsGainA double aL str3 starsLossA ///
					 bG str3 starsGain double bL str3 starsLoss ///
					 double alphaMin str4 starsAlpha ///
					 double alphaMax str4 starsAlphaMax ///
					 str4 cond double p ///
					 using "data/welfareTableRidgeAltBenchmark.dta", replace
					 
			foreach species of numlist 1/10 {
				preserve 
					
					keep if condition == `species'
		
					local benchmarkCase = 1
					replace vPost = vPost`benchmarkCase'	
				
					local benchmarkCase = 1	
					replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
					replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
					replace surplus = 0 if binarydec == 0
				
					cap drop betCE
					cap drop gain
					cap drop loss
					gen betCE = binarydec * surplus
					cap gen gain = .
					cap gen loss = . 
					replace gain = max(0, betCE)
					replace loss = min(0, betCE)

					
					sureg (gain win) /// 
							(loss win) 
					
					
					local mMin = 3
					local mMax = 30
					
					// Condition

					nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
								- ([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win))) ///
								(eq2: (([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win)))) ///	
								(eq3: ([gain]_cons + `mMin'*[gain]win)) ///
								(eq4: ([loss]_cons + `mMin'*[loss]win)) ///
								(eq5: (([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win)))) ///
								(eq6: ([gain]_cons + `mMax'*[gain]win)) ///
								(eq7: ([loss]_cons + `mMax'*[loss]win)) ///
							, post
					
					// New
					
					
					local bG = _b[eq6]
					local bL = _b[eq7]
					local seBg = _se[eq6] 
					local seBl = _se[eq7]
					
					test eq6 == 0
					local pGain = r(p)
					test eq7 == 0
					local pLoss = r(p)
					
					
					local starsGain = ""
					if (`pGain' <= 0.01) {
						local starsGain = "***"
					}
					if (`pGain' <= 0.05 & `pGain'>0.01) {
						local starsGain = "**"
					}
					if (`pGain' < 0.1 & `pGain'>0.05) {
						local starsGain = "*"
					}
					
					
					local starsLoss = ""
					if (`pLoss' <= 0.01) {
						local starsLoss = "***"
					}
					if (`pLoss' <= 0.05 & `pLoss'>0.01) {
						local starsLoss = "**"
					}
					if (`pLoss' < 0.1 & `pLoss'>0.05) {
						local starsLoss = "*"
					}
					
					
					
					
					// Previous
					
					local alphaMin = _b[eq2]
					local alphaSE = _se[eq2]
					local alphaMax = _b[eq5]
					local alphaMaxSE = _se[eq5]
					
					test eq2 == 0
					local pAlpha = r(p)
					
					local starsAlpha = ""
					if (`pAlpha' <= 0.01) {
						local starsAlpha = "***"
					}
					if (`pAlpha' <= 0.05 & `pAlpha'>0.01) {
						local starsAlpha = "**"
					}
					if (`pAlpha' < 0.1 & `pAlpha'>0.05) {
						local starsAlpha = "*"
					}

					local possibilityCond = "No"
					if (_b[eq1] >= 0) {
						local possibilityCond = "Yes"
					}
					
					
					test eq5 == 1
					local pAlphaMax = r(p)
					
					local starsAlphaMax = ""
					if (`pAlphaMax' <= 0.01) {
						local starsAlphaMax = "***"
					}
					if (`pAlphaMax' <= 0.05 & `pAlpha'>0.01) {
						local starsAlphaMax = "**"
					}
					if (`pAlphaMax' < 0.1 & `pAlpha'>0.05) {
						local starsAlphaMax = "*"
					}

					local possibilityCond = "No"
					if (_b[eq1] >= 0) {
						local possibilityCond = "Yes"
					}
					
					
					local aG = _b[eq3]
					local aGse = _se[eq3]
					local aL = _b[eq4]
					local aLse = _se[eq4]
					
					
					
					test _b[eq3] == 0
					local pGainA = r(p)
					test _b[eq4] == 0
					local pLossA = r(p)
					
					
					local starsGainA = ""
					if (`pGainA' <= 0.01) {
						local starsGainA = "***"
					}
					if (`pGainA' <= 0.05 & `pGainA'>0.01) {
						local starsGainA = "**"
					}
					if (`pGainA' < 0.1 & `pGainA'>0.05) {
						local starsGainA = "*"
					}
					
					
					local starsLossA = ""
					if (`pLossA' <= 0.01) {
						local starsLossA = "***"
					}
					if (`pLossA' <= 0.05 & `pLossA'>0.01) {
						local starsLossA = "**"
					}
					if (`pLossA' < 0.1 & `pLossA'>0.05) {
						local starsLossA = "*"
					}
					
					
					noisily: di "`possibilityCond'"
							
					test eq1
					local pTest = r(p)

					
				restore
				post welfareTable ///
					(`aG') ("`starsGainA'") (`aL') ("`starsLossA'")  ///
					(`bG') ("`starsGain'") (`bL') ("`starsLoss'") ///
					(`alphaMin') ("`starsAlpha'") (`alphaMax') ("`starsAlphaMax'") ///
					("`possibilityCond'") (`pTest')
								
				post welfareTable ///
					(`aGse') ("") (`aLse') ("") ///
					(`seBg') ("") (`seBl') ("") ///
					(`alphaSE') ("") (`alphaMaxSE') ("") ("") (.)
				

				
			}
			postclose welfareTable
			
			// Format the table (Supplementary Table A.4.D)
			preserve
				clear
				use "data/welfareTableRidgeAltBenchmark.dta"
								
				gen c1 = "("
				replace c1 = "" if mod(_n,2) == 1
				
				gen c2 = ") & ("
				replace c2 = "  &  " if mod(_n,2) == 1
				
				gen c3 = c2
				gen c4 = c2
				gen c5 = c2
				gen c6 = c2
				
				gen c7 = ") & "
				replace c7 = "  &  " if mod(_n,2) == 1
				
				gen c8 = " & " 
				
				gen c9 = " \\ "
				
				order c1 aG starsGainA c2 ///
						 aL starsLossA c3 ///
						 alphaMax starsAlphaMax c6 ///
						 bG starsGain  c4 ///
						 bL starsLoss  c5 /// 
						 alphaMin starsAlpha c7 ///
						 cond c8 p c9
				
				
				// Formatting the table
				
				/*
				Condition
				2 house crickets
				5 superworms
				3 silkworm pupae
				2 mole crickets
				2 field crickets
				*/
				
				gen rowlabel = "&"
				replace rowlabel = "2 house crickets	&" 	if mod(_n, 10) == 1 
				replace rowlabel = "5 superworms	&" 		if mod(_n, 10) == 3
				replace rowlabel = "3 silkworm pupae	&" 	if mod(_n, 10) == 5
				replace rowlabel = "2 mole crickets	&" 	if mod(_n, 10) == 7
				replace rowlabel = "2 field crickets	&" 	if mod(_n, 10) == 9
				
				local lines = _N
				local newLines = `lines' + 2
				di `newLines'
				set obs `newLines'
				
				replace rowlabel = "\textit{Video} \\" 		if _n == 21
				replace rowlabel = "\textit{No Video} \\" 	if _n == 22
				
				gen location = _n
				replace location = 0 		if location == 21
				replace location = 10.5 	if location == 22
				
				drop if location > 10
				drop if location == 0

				sort location
				drop location
				order rowlabel
				
				
				format aG aL bG bL alphaMin alphaMax p %9.3f
				export delimited using "../writeup/tables/SupplementaryTableA4D.tsv", delimiter(tab)  datafmt novar replace
								
			restore
	}
	
	
}	
	

